package tec.stan.ling.core.service;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;

import tec.stan.ling.core.top.ex.AbstractTopoService;
import tec.stan.ling.core.top.ex.ITopoService;

public class ServiceManager {

	public static final String TOPO_EXTENSION_ID = "tec.stan.ling.core.topo";

	private final static String ATTRI_ID = "id";

	private final static String ATTRI_NAME = "name";

	private final static String ATTRI_CLASS = "class";

	private ITopoService topoService = null;

	public void load() {
		AbstractTopoService  topo= (AbstractTopoService)getTopoService();
		topo.preLoad();
		
		topo.loadService();
		
		topo.postLoad();
	}

	public ITopoService getTopoService() {
		if (topoService == null) {
			IConfigurationElement[] cef = Platform.getExtensionRegistry()
					.getConfigurationElementsFor(TOPO_EXTENSION_ID);
			for (IConfigurationElement ce : cef) {
				try {
					Object cee = ce.createExecutableExtension(ATTRI_CLASS);
					if (cee instanceof AbstractTopoService) {
						AbstractTopoService ms = (AbstractTopoService) cee;
						ms.setId(ce.getAttribute(ATTRI_ID));
						ms.setName(ce.getAttribute(ATTRI_NAME));
						topoService = ms;
						break;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return topoService;

	}

}
